home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 August: Tool Chest / Dev.CD Aug 00 TC Disk 2.toast / pc / sample code / interapplication comm / aecdev.aedaemon / aecdev.c next >
Encoding:
C/C++ Source or Header  |  2000-06-23  |  10.2 KB  |  217 lines

  1. /*
  2.     File:        AECdev.c
  3.  
  4.     Contains:    AECDEV demonstrates the techniques needed to send AppleEvents
  5.                 from a CDEV/DA/INIT/Driver.
  6.                 Requires the sample AEDaemon to work.
  7.     
  8.                 The techniques used in AECdev are the same you need to use to send AppleEvents
  9.                 from any non-application piece of code, DAs/INIT/Drivers, or anything else.
  10.                 Since the High Level Event manager will not let anyone post High Level Events 
  11.                 unless they have an event loop, your non-event looped code must rely
  12.                 on a 'buddy' (in this case AEDaemon) to do the sending for you.
  13.                 There's not really much more code to write (just some PPC code), it's
  14.                 mainly a matter of moving the code from the CDEV to the backgrounder.
  15.     
  16.                 Using a buddy and PPC can give you all the functionality of AppleEvents.
  17.  
  18.     Written by: C.K. Haun    
  19.  
  20.     Copyright:    Copyright © 1991-1999 by Apple Computer, Inc., All Rights Reserved.
  21.  
  22.                 You may incorporate this Apple sample source code into your program(s) without
  23.                 restriction. This Apple sample source code has been provided "AS IS" and the
  24.                 responsibility for its operation is yours. You are not permitted to redistribute
  25.                 this Apple sample source code as "Apple sample source code" after having made
  26.                 changes. If you're going to re-distribute the source, we require that you make
  27.                 it clear in the source that the code was descended from Apple sample source
  28.                 code, but that you've made changes.
  29.  
  30.     Change History (most recent first):
  31.                 7/20/1999    Karl Groethe    Updated for Metrowerks Codewarror Pro 2.1
  32.                 
  33.  
  34. */
  35. /*------------------------------------------------------------------------------
  36. #   This file contains the basic CDEV controlling code
  37. ------------------------------------------------------------------------------*/
  38.  
  39. #include "AECdev.h"
  40.  
  41. /* This is the main dispatcher. It must be the first code in the cdev.
  42. AECDEV's dispatcher responds only to the following messages from
  43. the Control Panel:
  44.  
  45. macDev      - To indicate what machines it is available on.
  46. initDev     - To set up some temporary storage and get the caret started.
  47.  
  48. */
  49. pascal Handle AECDEV(short message, short item, short numItems, short CPanelID, EventRecord *theEvent, Handle cdevStorage,
  50.                      DialogPtr CPDialog)
  51. {
  52.     CDEVPtr temp;
  53. #pragma unused (CPanelID,theEvent)     /* unused formal parameters */    
  54.     if (message == macDev)
  55.         return((Handle)1);                                  /* we work on every machine */
  56.     else if (cdevStorage != nil) {
  57.         switch (message) {
  58.             long result;
  59.             case initDev:                                   /* initialize cdev */
  60.                 cdevStorage = NewHandle(sizeof(CDEVRec));       /* create private storage */
  61.                 /* get a PPC block.  If we can't allocate one, we'll just be dead */
  62.                 if (cdevStorage) {
  63.                     HLock(cdevStorage);
  64.                     temp = (CDEVPtr)*cdevStorage;
  65.                     temp->myPPCBlock = (MyPPCRecPtr)NewPtrClear(sizeof(MyPPCRec));
  66.                     if (temp->myPPCBlock) {
  67.                         temp->myPPCBlock->myPort = (PPCPortPtr)NewPtrClear(sizeof(PPCPortRec));
  68.                         if (temp->myPPCBlock->myPort) {
  69.                         /* set the default settings for my blocks */
  70.                             temp->myPPCBlock->buffer = nil;
  71.                             temp->myPPCBlock->bufferSize = nil;
  72.                             temp->myPPCBlock->ourPort = nil;
  73.                             temp->myPPCBlock->dataToXfer = nil;
  74.                             temp->myPPCBlock->currentSessionRef = nil;
  75.                             temp->searchForTarget = false;
  76.                             temp->noBuddy = false;
  77.                             temp->notSys7 = false;
  78.                             temp->eventPending = false;
  79.                             HUnlock(cdevStorage);
  80.                             HiliteControl(SnatchHandle(CPDialog, numItems + 1), 255);
  81.                             /* check Gestalt to see if we're on an AppleEvent/PPC */
  82.                             /* capable machine machine */
  83.                             if ((Gestalt(gestaltAppleEventsAttr, &result) != noErr) ||
  84.                                 (Gestalt(gestaltPPCToolboxAttr, &result) != noErr)) {
  85.                                 /* no PPC or no AppleEvents.  Forget it */
  86.                                 temp = (CDEVPtr)*cdevStorage;
  87.                                 temp->notSys7 = true;
  88.                             } else {
  89.                             /* Start PPC for us */
  90.                                 if (PPCInit() == noErr) {
  91.                                     FindAEBuddy((CDEVHnd)cdevStorage);/* find and launch AEBud */
  92.                                 } else {
  93.                                 /* PPC didn't init correctly, bail out */
  94.                                     temp = (CDEVPtr)*cdevStorage;
  95.                                     temp->notSys7 = true;
  96.                                 }
  97.                             }
  98.                             temp = (CDEVPtr)*cdevStorage;
  99.                             /* Alert user that we con't find our Buddy */
  100.                             if (temp->noBuddy)
  101.                                 StopAlert(kNoBuddyAlert, nil);
  102.                         } else {                            /* PPC Port rec mem check */
  103.                             /* if this fails, kill the other pointer also */
  104.                             DisposePtr((Ptr)temp->myPPCBlock);
  105.                             temp->myPPCBlock = nil;
  106.                             DisposeHandle(cdevStorage);
  107.                             /* and have Control Panel manager say out of mem */
  108.                             cdevStorage = nil;
  109.                         }
  110.                     } else {                                /* block mem check */
  111.                         DisposeHandle(cdevStorage);
  112.                         /* and have Control Panel manager say out of mem */
  113.                         cdevStorage = nil;
  114.                     }
  115.                 }                                           /* cdevStorage mem check */
  116.                 break;
  117.             case nulDev:
  118.                 /* check here all the time */
  119.                 /* for the port coming up if we had to launch the sender */
  120.                 /* also dim the control, as necessary */
  121.                 /* Can't do this in our completion routine, since it requires calling the */
  122.                 /* COntrol Manager and the Memory Manager */
  123.                 HLock((Handle)cdevStorage);
  124.                 temp = (CDEVPtr)*cdevStorage;                
  125.                 if (temp->eventPending || (temp->searchForTarget) || temp->myPPCBlock->currentSessionRef) {
  126.                     HiliteControl(SnatchHandle(CPDialog, numItems + kSendButton), 255);
  127.                     if (!temp->notSys7)
  128.                         FindAEBuddy((CDEVHnd)cdevStorage);
  129.                 } else {
  130.                     /* if the buddy is not up yet, or if it could not be found at all */
  131.                     /* dim the button out. */
  132.                     if (temp->noBuddy || temp->notSys7)
  133.                         HiliteControl(SnatchHandle(CPDialog, numItems + kSendButton), 255);
  134.                     else
  135.                         HiliteControl(SnatchHandle(CPDialog, numItems + kSendButton), 0);
  136.                 }
  137.                 /* also, there's .... I forgot what I was going to say */
  138.                 /* Oh yeah, I remember.  I also need to check here periodically to */
  139.                 /* see if the PPC calls have completed so I can dispose of the */
  140.                 /* data handle that I allocated for the PPC XFer process. */
  141.                 if (!temp->myPPCBlock->pB.openParam.portRefNum && temp->myPPCBlock->buffer != nil && !temp->noBuddy) {
  142.                     DisposePtr(temp->myPPCBlock->buffer);
  143.                     temp->myPPCBlock->buffer = nil;
  144.                     temp->eventPending = false;
  145.                 }
  146.                 HUnlock((Handle)cdevStorage);
  147.                 break;
  148.                 
  149.             case hitDev:   /* handle hit on item */
  150.             /* They clicked in our button.  Find our Buddy ( it is possible that it  */
  151.             /* terminated while we were waiting, this will re-launch it if that happened ) */
  152.             
  153.                 if ((item - numItems) == kSendButton) {
  154.                     /* our send button.  Do it now */
  155.                     FindAEBuddy((CDEVHnd)cdevStorage);                    
  156.                     FindATarget((CDEVHnd)cdevStorage);
  157.                 }
  158.                 break;
  159.             case closeDev:                                  /* clean up and dispose */
  160.                 temp = (CDEVPtr)*cdevStorage;
  161.                 if (temp->myPPCBlock->ourPort) {
  162.                     /* we have a port open.  Close the port, blowing off any pending */
  163.                     /* writes or whatever */
  164.                     PPCClosePBPtr closeRec = (PPCClosePBPtr)NewPtrClear(sizeof(PPCClosePBRec));
  165.                     closeRec->ioCompletion = nil;
  166.                     closeRec->ioResult = 0;
  167.                     closeRec->portRefNum = temp->myPPCBlock->ourPort;
  168.                     PPCClose(closeRec, false);
  169.                     DisposePtr((Ptr)closeRec);
  170.                                 }
  171.                     /* and kill our memory */
  172.                     if (temp->myPPCBlock->myPort)DisposePtr((Ptr)temp->myPPCBlock->myPort);
  173.                     if (temp->myPPCBlock)DisposePtr((Ptr)temp->myPPCBlock);
  174.                     
  175.                 break;
  176.                 
  177.             case updateDev:                                 /* handle any update drawing */
  178.             case activDev:                                  /* activate any needed items */
  179.             case deactivDev:                                /* deactivate any needed items */
  180.                 break;
  181.                 
  182.             case keyEvtDev:                                 /* respond to keydown */
  183.                 break;
  184.                 
  185.             case macDev:
  186.             case undoDev:
  187.                 break;
  188.                 
  189.             case cutDev:
  190.             case copyDev:
  191.             case pasteDev:
  192.             case clearDev:
  193.                 break;
  194.         }
  195.         return(cdevStorage);
  196.     }  /* cdevStorage != nil */
  197.     
  198.     /* 
  199.     **  if cdevStorage = NIL then ControlPanel 
  200.     **  will put up memory error
  201.     */
  202.     return(nil);
  203. }
  204.  
  205. /* Gets the ControlHandle for the item you want in the dialog box thebox.  */
  206. /* Handy for setting checkboxes and radio buttons */
  207. ControlHandle SnatchHandle(DialogPtr thebox, short theGetItem)
  208. {
  209.     short itemtype;
  210.     Rect itemrect;
  211.     Handle thandle;
  212.     GetDialogItem(thebox, theGetItem, &itemtype, &thandle, &itemrect);
  213.     return((ControlHandle)thandle);
  214. }
  215.  
  216.  
  217.